﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LBoL.Base;
using LBoL.ConfigData;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.Core.Battle;
using LBoL.Core.Cards;
using LBoL.Core;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards;
using ProbeMod.Cards.Template;

namespace ProbeMod.Cards
{
    /// <summary>
    /// 阴影投射
    /// </summary>
    public sealed class ProbeShadeProjectionDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Uncommon;
            config.Type = CardType.Skill;
            config.TargetType = TargetType.Nobody;

            config.Colors = new List<ManaColor>() { ManaColor.Blue };
            config.Cost = new ManaGroup() { Blue = 2 };

            config.Value1 = 1;
            config.Value2 = 2;

            config.Mana = new ManaGroup { Any = 0 };

            config.Keywords = Keyword.Exile;
            config.UpgradedKeywords = Keyword.Exile;

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    /// <summary>
    /// 选择手牌中一张同伴牌，获取它的1/2张1耐0费复制
    /// </summary>
    [EntityLogic(typeof(ProbeShadeProjectionDef))]
    public sealed class ProbeShadeProjection : ProbeCard
    {
        /// <summary>
        /// 复制的卡牌数量
        /// </summary>
        public int CardCount
        {
            get => IsUpgraded ? Value2 : Value1;
        }

        public override Interaction Precondition()
        {
            List<Card> list = base.Battle.HandZone.Where((Card hand) => hand != this && hand.CanBeDuplicated && hand.Config.Type == CardType.Friend).ToList<Card>();
            if (list.Count <= 0)
            {
                return null;
            }
            return new SelectHandInteraction(1, 1, list);
        }

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            if (precondition != null)
            {
                Card origin = ((SelectHandInteraction)precondition).SelectedCards[0];
                List<Card> list = new List<Card>();
                for (int i = 0; i < CardCount; i++)
                {
                    Card card = origin.CloneBattleCard();
                    card.Loyalty = 1;
                    card.SetTurnCost(base.Mana);
                    list.Add(card);
                }
                yield return new AddCardsToHandAction(list);
            }
        }
    }
}
